package com.cljh.service.impl;

import com.cljh.bean.Product;
import com.cljh.bean.Sale;
import com.cljh.bean.SaleTable;
import com.cljh.controller.page.PageBean;
import com.cljh.controller.page.ProductPageBean;
import com.cljh.mapper.ProductMapper;
import com.cljh.mapper.ProductsMapper;
import com.cljh.mapper.SaleMapper;
import com.cljh.service.SaleService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    SaleMapper saleMapper;


    @Override
    public PageBean<SaleTable> selectAllByCondition(Integer currentPage, Integer pageSize, SaleTable saleTable) {
        String username = saleTable.getUsername();
        if (username!=null&&username.length()>0){
            saleTable.setUsername("%"+username+"%");
        }
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        List<SaleTable> saleTables = saleMapper.selectAllByCondition(begin, pageSize, saleTable);
        int i = saleMapper.selectCount(saleTable);

        PageBean<SaleTable> saleTablePageBean = new PageBean<>();
        saleTablePageBean.setTotalCount(i);
        saleTablePageBean.setRows(saleTables);
        return saleTablePageBean;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteId(Integer id) {
        saleMapper.deleteByIdInt(id);
        return saleMapper.deleteId(id)>0;
    }

    @Override
    public boolean upd(SaleTable saleTable) {
        return saleMapper.upd(saleTable)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAll(List<Integer> ids) {
        for (Integer i :
                ids) {
            saleMapper.deleteByIdInt(i);
        }

        return saleMapper.deleteAll(ids)>0;
    }

    @Override
    public SaleTable selectBySaleId(Integer id) {
        return saleMapper.selectBySaleId(id);
    }

    @Override
    public List<SaleTable> selectAll() {
        return saleMapper.selectAll();
    }

    @Override
    public List<Sale> selectSale() {
        return saleMapper.selectSale();
    }

    @Override
    public List<Sale> selectAllSales(String types) {
        return saleMapper.selectAllSales(types);
    }

    @Override
    public List<SaleTable> selectByUsername(String username) {
        return saleMapper.selectByUsername(username);
    }

    @Autowired
    ProductsMapper productsMapper;
    @Autowired
    ProductMapper productMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveSaleTable(SaleTable saleTable, @NotNull List<ProductPageBean> productPageBeans, Integer userId) {
        //添加订单表信息
        int i = saleMapper.saveSaleTable(saleTable);
        //获取订单id
        Integer id = saleTable.getId();
        //创建订单商品集合
        List<Sale> list = new ArrayList<>();
        for (ProductPageBean s : productPageBeans) {
            Sale sale = new Sale();
            sale.setTableId(id);
            sale.setSum(Long.valueOf(s.getProductSum()));
            sale.setMoney(s.getPrice());
            sale.setProductName(s.getProductName());
            sale.setTypes(s.getTypes());
            list.add(sale);
            //获取商品id
            Integer productId = s.getId();
            //删除购物车信息
            productsMapper.deleteByUserAndProduct(userId, productId);
            Product product = productMapper.selectById(productId);

            long productSum = product.getProductSum()-s.getProductSum();
            product.setProductSum(productSum);
            productMapper.upd(product);
        }
        int i1 = saleMapper.saveSales(list);

        return (i1>0&&i>0);
    }
}
